/**
 * Created by zzy on 2017/4/11.
 */
var $ = require('jquery');
require('easyui');
var $alert = require('libs/alert');
require('fancybox');
$(function () {
    //获取url参数
    var id = $.getUrlParam("id");
    $(document).on('click','a[name=packageDetail]',function () {
        var boxId = $(this).data('id');
        $.get('api/v1/p/mms/order/boxGoodsDetail',{boxId:boxId},function (res) {
            if(res.code == 200){
                $.fancybox.open({
                    type : 'inline',
                    src  : '#packageFancy',
                    transitionEffect  : "fade",
                    afterShow: function () {
                        $('#packageTable').datagrid('loadData',{total:res.data.length,rows:res.data});
                    }
                });
            }else{
                $alert(res.message);
            }
        });
    });
    $(document).on('click','a[name=orderDetail]',function () {
        window.parent.tabOpen($(this).attr('ref'),'入库单详情',true,true);
    });
    fillData(id);
    function fillData(id) {
        $.get('api/v1/p/mms/trade/load',{id: id},function (res) {
            if(res.code == 200){
                var data = res.data;
                var $firstTarget = $("#baseInfo").find("span");
                var tradeStatus = {
                    "new": "新建",
                    "processing": "处理中",
                    "completed": "处理完成"
                };
                //元素赋值
                $firstTarget.eq(0).text(data.entName);
                $firstTarget.eq(1).text(data.currency);
                $firstTarget.eq(2).text(data.tradeDt);
                $firstTarget.eq(3).text(tradeStatus[data.tradeStatus]);
                $firstTarget.eq(4).text(data.totalVolumn);
                $firstTarget.eq(5).text(data.totalWeight);
                $firstTarget.eq(6).text(data.totalBoxNum);
                $firstTarget.eq(7).text(data.totalGoodsNum);
                $firstTarget.eq(8).text(data.totalDeclaredValue);
                $firstTarget.eq(9).text(data.totalAmount);
                tableInit(data.relEntityId);
            }else{
                $alert(res.message);
            }
        });
    }
    function tableInit(manifestId) {
        $('#packageTable').datagrid({
            border : false,
            data: [],
            rownumbers:true,
            emptyMsg: '暂无数据',
            columns:[[
                {
                    field:'goodsCode',
                    title:'商品编码',
                    width:240
                },{
                    field:'goodsName',
                    title:'商品名称',
                    width:240
                },
                {
                    field:'num',
                    title:'商品数量',
                    width:210
                }
            ]]
        });
        $("#mainTable").datagrid({
            url:'api/v1/p/mms/manifest/item/list',
            border: false,
            pagination: true,
            pageSize: 10,
            pageList: [10, 20, 50, 100],
            singleSelect: true,
            rownumbers: true,
            emptyMsg: '暂无数据',
            queryParams: {
                manifestId: manifestId
            },
            columns:[[
                {
                    field : 'boxCode',
                    title : '包裹码',
                    width : '25%',
                    formatter:function (value,row,index) {
                        return "<a href='javascript:;' name='packageDetail' data-id='"+row.boxId+"'>"+value+"</a>"
                    }
                },{
                    field : 'orderNo',
                    title : '入库单号',
                    width : '25%',
                    formatter:function (value,row,index) {
                        return "<a href='javascript:;' ref='in-order-detail?id="+row.orderId+"' name='orderDetail'>"+value+"</a>"
                    }
                },{
                    field : 'size',
                    title : '尺寸(CM)',
                    width : '25%',
                    formatter:function (value,row,index) {
                        var l,w,h;
                        l = row.factLength || row.regLength;
                        w = row.factWidth || row.regWidth;
                        h = row.factHigh || row.regHigh;
                        return l+'×'+w+'×'+h;
                    }
                },{
                    field : 'weight',
                    title : '重量(Kg)',
                    width : '25%',
                    formatter:function (value,row,index) {
                        return row.feeWeight || row.regWeight;
                    }
                }
            ]],
            onClickRow: function (rowIndex, rowData) {
                $(this).datagrid('unselectRow', rowIndex);
            },
            loadFilter:function(data){
                if(data.code == '200'){
                    return data.data;
                }else{
                    return [];
                }
            }
        });
    }
});
